package com.zzptc.sgz.shop.controller;

import com.zzptc.sgz.shop.dao.ProductDaoI;
import com.zzptc.sgz.shop.dao.ProductDaoImpl;
import com.zzptc.sgz.shop.dao.UserDaoI;
import com.zzptc.sgz.shop.dao.UserDaoImpl;
import com.zzptc.sgz.shop.vo.Product;
import com.zzptc.sgz.shop.vo.User;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.List;

@WebServlet("/login")
public class LoginServlet extends javax.servlet.http.HttpServlet {
    protected void doPost(javax.servlet.http.HttpServletRequest request,
                          javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, IOException {
        doGet(request,response);
    }

    protected void doGet(javax.servlet.http.HttpServletRequest request,
                         javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        String uname=request.getParameter("userName");
        String upass=request.getParameter("passWord");
        System.out.println(uname+upass);
        UserDaoI udao=new UserDaoImpl();
        User u=null;
        try {
             u=udao.getUser(uname,upass);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        if(u!=null){
            HttpSession session = request.getSession();
            session.setAttribute("user",u);
            System.out.println("登陆成功");
        }else{
            System.out.println("登陆失败");
        }

        response.sendRedirect("/ProductServlet");

    }
}
